package resource.handler;

import items.ItemOnMap;

import java.util.ArrayList;

import launch.SimpleTest;
import map.Map;

import org.newdawn.slick.util.Log;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import character.Hero;

import filters.FilterCollision;
import filters.FilterScale;
import filters.FilterEvent;

/**
 * Parser SAX
 * @author Kent
 *
 */
public class MapHandler extends DefaultHandler {

	private ArrayList<Map> l_map;

	public MapHandler(ArrayList<Map> s){
		l_map = s;
	}

	public void characters(char[] buffer, int start, int length) {
	}

	public void startElement(String uri, String name, String qName, Attributes attributes){
		if (qName.equals("map")){
			try {
				Log.info("Chargement Map : " + attributes.getValue("name"));
				if(attributes.getValue("script") == null){
					l_map.add(new Map(attributes.getValue("name"),attributes.getValue("url"),Integer.parseInt(attributes.getValue("width")),Integer.parseInt(attributes.getValue("height"))));
				}else{
					l_map.add(new Map(attributes.getValue("name"),attributes.getValue("url"),Integer.parseInt(attributes.getValue("width")),Integer.parseInt(attributes.getValue("height")),attributes.getValue("script")));
				}
			} catch (Exception e) {
				e.printStackTrace();
			};
		};

		if (qName.equals("filter")){
			try {
				if(attributes.getValue("name").equals("collision")){
					FilterCollision fc = new FilterCollision(attributes.getValue("filter"));
					l_map.get(l_map.size()-1).addFilter(attributes.getValue("name"), fc);
				}else if(attributes.getValue("name").equals("scale")){
					FilterScale fs = new FilterScale(attributes.getValue("filter"));
					l_map.get(l_map.size()-1).addFilter(attributes.getValue("name"), fs);
				}else if(attributes.getValue("name").equals("event")){
					FilterEvent fe = new FilterEvent(attributes.getValue("filter"));
					l_map.get(l_map.size()-1).addFilter(attributes.getValue("name"), fe);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		if(qName.equals("position_init")){
			try {
				Hero gc = SimpleTest.gc_manager.getGameCharacter(attributes.getValue("name"));
				gc.setY(Integer.parseInt(attributes.getValue("y")));
				gc.setX(Integer.parseInt(attributes.getValue("x")));
				boolean standing;
				if(attributes.getValue("type").equals("false")){
					standing = false;
				}else{
					standing = true;
				}
				gc.setInitOn(l_map.get(l_map.size()-1).getName(),Integer.parseInt(attributes.getValue("x")),Integer.parseInt(attributes.getValue("y")));
				gc.setPositionOn(l_map.get(l_map.size()-1).getName(),standing, attributes.getValue("type"));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if(qName.equals("item")){
			ItemOnMap iom = (ItemOnMap)SimpleTest.inventory_manager.getItem(attributes.getValue("name"));
			iom.x = Integer.parseInt(attributes.getValue("x"));
			iom.y = Integer.parseInt(attributes.getValue("y"));
			iom.setProprietaire(l_map.get(l_map.size()-1));
		}
	}

	// When the parser encounters the end of an element, it calls this method
	public void endElement(String uri, String name, String qName){

	}

	/** This method is called when warnings occur */
	public void warning(SAXParseException exception) {
		System.err.println("WARNING: line " + exception.getLineNumber() + ": "
				+ exception.getMessage());
	}

	/** This method is called when errors occur */
	public void error(SAXParseException exception) {
		System.err.println("ERROR: line " + exception.getLineNumber() + ": "
				+ exception.getMessage());
	}

	/** This method is called when non-recoverable errors occur. */
	public void fatalError(SAXParseException exception) throws SAXException {
		System.err.println("FATAL: line " + exception.getLineNumber() + ": "
				+ exception.getMessage());
		throw (exception);
	}
}